package com.aos.service.application.impl;

import com.aos.dao.ApplicationDao;
import com.aos.dto.ApplicationDto;
import com.aos.entity.Application;
import com.aos.properties.APMSProperties;
import com.aos.service.application.ApplicationService;
import com.aos.util.CopyUtils;
import com.aos.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ApplicationServiceImpl
 * @Description:应用APP
 * @Author wumingwang
 * @Date 2018/9/5 17:15
 * @Version 1.0
 */
@Service
@Slf4j
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private ApplicationDao applicationDao;

    @Autowired
    private APMSProperties apmsProperties;

    @Override
    public Result queryByPage(Map<String, Object> map) {
        /**开启分页查询，sql的执行要紧跟这个分页工具*/
        PageHelper.startPage((int)map.get("pageNum"),(int)map.get("pageSize"));
        List<ApplicationDto> list =  applicationDao.queryByPage(map);
        PageInfo<ApplicationDto> p = new PageInfo<>(list);
        return Result.ok(p);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addApplication(Application application) {
        if(applicationDao.queryForCheckByCode(application) != null){
            return Result.error("应用code已存在！");
        }
        if(applicationDao.queryForCheckByName(application) != null){
            return Result.error("应用名称已存在！");
        }
        application.setCreatedTimestamp(new Date());
        application.setUpdatedDatetime(application.getCreatedTimestamp());
        applicationDao.insert(application);
        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateStateForDelete(Long id) {
        Application application = new Application();
        application.setId(id);
        application.setState(apmsProperties.getApplication().getStateDelete());
        application.setUpdatedDatetime(new Date());
        applicationDao.updateNotNull(application);
        return Result.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(Application application) {
        Application oldapp = applicationDao.queryById(application.getId());
        if(oldapp == null){
            return Result.error("要更新的应用不存在！");
        }
        if(applicationDao.queryForCheckByCode(application) != null){
            return Result.error("应用code已存在！");
        }
        if(applicationDao.queryForCheckByName(application) != null){
            return Result.error("应用名称已存在！");
        }
        try{
            CopyUtils.copy(application,oldapp);
        }catch (Exception e){
            log.error("属性复制错误！",e);
            return Result.error("系统繁忙，请稍后再试！");
        }
        oldapp.setUpdatedDatetime(new Date());
        applicationDao.update(oldapp);
        return Result.ok();
    }

    @Override
    public Result queryDetails(Long id) {
        return Result.ok(applicationDao.queryDetailsById(id));
    }

    @Override
    public Result queryAll() {
        return Result.ok(applicationDao.queryAll());
    }
}
